package priv.mill.nio;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class FileTest {

	public static void main(String[] args) throws IOException {
		Path path = Paths.get("/Users/mill/Documents/cc.txt");
		FileChannel file = FileChannel.open(path, StandardOpenOption.WRITE,StandardOpenOption.CREATE_NEW);
		ByteBuffer srcs = ByteBuffer.wrap("this is my info for nio".getBytes());
		file.write(srcs);
	}

	/**
	 * 读取文件
	 */
	private static void readFile() {
		Path path = Paths.get("/Users/mill/Documents/aa.sql");
		OpenOption options = StandardOpenOption.READ;
		try(FileChannel open = FileChannel.open(path, options);){
			ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
			while(open.read(byteBuffer)!=-1) {
				byteBuffer.flip();
				System.out.println(new String(byteBuffer.array(),0,byteBuffer.limit()));
				byteBuffer.clear();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	

}
